package com.yn.club.auth.application.controller;

import com.yn.club.auth.application.dto.AuthPermissionDTO;
import com.yn.club.auth.domain.entity.AuthPermissionDO;
import com.yn.club.auth.domain.service.AuthPermissionDomainService;
import javax.validation.constraints.NotBlank;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.List;
import com.yn.club.common.util.R;
import com.yn.club.common.util.PageReq;
import com.yn.club.common.util.PageResp;
import com.yn.club.auth.application.convert.AuthPermissionApplicationConverter;
import com.yn.club.auth.application.request.AuthPermissionQueryRequest;
import com.yn.club.auth.application.request.AuthPermissionSaveRequest;

/**
 * (AuthPermission)表控制层
 *
 * @author YN
 * @since 2025-01-22 16:44:31
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("authPermission")
public class AuthPermissionController {
    /**
     * 服务对象
     */
    private final AuthPermissionDomainService authPermissionDomainService;
    private final AuthPermissionApplicationConverter authPermissionApplicationConverter;

    /**
     * 分页查询数据
     *
     * @param pageReq 分页参数
     * @param query   查询条件
     * @return 所有数据
     */
    @GetMapping("/page")
    public R<PageResp<AuthPermissionDTO>> queryPage(PageReq pageReq, AuthPermissionQueryRequest query) {
        AuthPermissionDO authPermissionDO = authPermissionApplicationConverter.convertQueryRequestToDo(query);
        PageResp<AuthPermissionDO> page = authPermissionDomainService.page(pageReq, authPermissionDO);
        
        List<AuthPermissionDTO> authPermissionDTOS = authPermissionApplicationConverter.convertDoToDto(page.getList());
        return R.ok(PageResp.of(page,authPermissionDTOS));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R<AuthPermissionDTO> selectOne(@PathVariable Serializable id) {
    AuthPermissionDTO authPermissionDTO = authPermissionApplicationConverter.convertDoToDto(
      this.authPermissionDomainService.getById(id));
     
    return R.ok(authPermissionDTO);
    }

    /**
     * 新增数据
     *
     * @param authPermission 实体对象
     * @return 受影响条数
     */
    @PostMapping("/insert")
    public R<Boolean> insert(@RequestBody @Validated AuthPermissionSaveRequest authPermission) {
       AuthPermissionDO authPermissionDO = authPermissionApplicationConverter.convertSaveRequestToDo(authPermission);
        return R.ok(this.authPermissionDomainService.insert(authPermissionDO));
    }

    /**
     * 修改数据
     *
     * @param authPermission 实体对象
     * @return 受影响条数
     */
    @PutMapping("/update")
    public R<Boolean> update(@RequestBody @Validated AuthPermissionSaveRequest authPermission) {
        AuthPermissionDO authPermissionDO = authPermissionApplicationConverter.convertSaveRequestToDo(authPermission);
        return R.ok(this.authPermissionDomainService.updateById(authPermissionDO));
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 受影响条数
     */
    @DeleteMapping("/delete")
    public R<Boolean> delete(@RequestBody @NotEmpty List<Integer> ids) {
        return R.ok(this.authPermissionDomainService.delete(ids));
    }

    /**
     * 查询用户权限
     */
    @GetMapping("getPermission")
    public R<Boolean> getPermissionByName(@NotBlank(message = "用户名称不能为空") String userName) {
            log.info("PermissionController.getPermission.userName:{}",userName);
            return R.ok(authPermissionDomainService.getPermissionByName(userName));
    }

}

